package com.csii.adhoc.controller.info;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csii.adhoc.annotation.OperationLog;
import com.csii.adhoc.info.user.BatchUpdateUserVo;
import com.csii.adhoc.info.user.UserPo;
import com.csii.adhoc.info.user.UserVo;
import com.csii.adhoc.info.user.service.UserService;
import com.csii.adhoc.util.DateUtils;
import com.csii.result.APIResult;
import com.csii.result.PageVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @description: 用户接口控制层
 * @author: panChuanJiang
 * @create: 2021-01-25 16:27
 */
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @RequestMapping("userMaintain")
    @OperationLog("用户维护")
    public APIResult<?> userMaintain(@RequestBody UserVo userVo) {
        String userId = Optional.ofNullable(String.valueOf(SecurityUtils.getSubject().getPrincipal())).orElse("未知用户");
        userVo.setUpdatedBy(userId)
                .setUpdatedDt(DateUtils.getDateString("yyyyMMddHHmmss"));
        userService.userMaintain(userVo, userVo.getOptMode());
        return APIResult.successResult(null);
    }

    @RequestMapping("userQuery")
    public APIResult<PageVo> userQuery(@RequestBody(required = false) UserVo userVo) {
        if (userVo != null){
            Integer pageNo = userVo.getPageNo();
            Integer pageSize = userVo.getPageSize();
            if (pageNo != null && pageSize != null && pageSize != 0) {
                Page<UserPo> page = new Page<>(pageNo, pageSize);
                IPage<UserPo> userPoIPage = userService.selectPageUser(page, userVo);
                List<UserPo> userPos = userPoIPage.getRecords();
                userPos.forEach(x -> {
                    x.setPassword(null).setSalt(null);
                });
                return APIResult.successResult(PageVo.getPageVoFromIPage(userPoIPage));
            }
        }
        PageVo<UserPo> vo = new PageVo();
        List<UserPo> userPos = userService.selectListUser(userVo);
        vo.setRecords(userPos);
        return APIResult.successResult(vo);
    }

    /**
     * 批量更新用户的角色或者职务
     * @param batchUpdateUserVo
     * @return
     */
    @RequestMapping("updateListUserRoleOrPost")
    @OperationLog("批量设置用户角色或职务")
    public APIResult<?> updateListUserRoleOrPost(@RequestBody BatchUpdateUserVo batchUpdateUserVo) {
        userService.updateListUserRoleOrPost(batchUpdateUserVo);
        return APIResult.successResult(null);
    }

    /**
     * 修改用户密码
     */
    @RequestMapping("updateUserPassword")
    @OperationLog("修改用户密码")
    public APIResult<?> updateUserPassword(@RequestBody UserVo userPo) {
        userService.updateUserPassword(userPo);
        return APIResult.successResult(null);
    }
}
